package com.ruoyi.web.controller;

import java.util.List;

import com.ruoyi.web.controller.Util.FileUploadUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PictureText;
import com.ruoyi.system.service.IPictureTextService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图文Controller
 *
 * @author ruoyi
 * @date 2022-09-12
 */
@Controller
@RequestMapping("/system/text")
public class PictureTextController extends BaseController {
    private String prefix = "system/text";

    @Autowired
    private IPictureTextService pictureTextService;

    @RequiresPermissions("system:text:view")
    @GetMapping()
    public String text() {
        return prefix + "/text";
    }

    /**
     * 查询图文列表
     */
    @RequiresPermissions("system:text:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PictureText pictureText) {
        startPage();
        List<PictureText> list = pictureTextService.selectPictureTextList(pictureText);
        return getDataTable(list);
    }

    /**
     * 导出图文列表
     */
    @RequiresPermissions("system:text:export")
    @Log(title = "图文", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PictureText pictureText) {
        List<PictureText> list = pictureTextService.selectPictureTextList(pictureText);
        ExcelUtil<PictureText> util = new ExcelUtil<PictureText>(PictureText.class);
        return util.exportExcel(list, "图文数据");
    }

    /**
     * 新增图文
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存图文
     */
    @Log(title = "图文", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PictureText pictureText) {
        return toAjax(pictureTextService.insertPictureText(pictureText));
    }

    /**
     * 修改图文
     */
    @RequiresPermissions("system:text:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        PictureText pictureText = pictureTextService.selectPictureTextById(id);
        mmap.put("pictureText", pictureText);
        return prefix + "/edit";
    }

    /**
     * 修改保存图文
     */
    @RequiresPermissions("system:text:edit")
    @Log(title = "图文", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PictureText pictureText) {
        return toAjax(pictureTextService.updatePictureText(pictureText));
    }

    /**
     * 删除图文
     */
    @RequiresPermissions("system:text:remove")
    @Log(title = "图文", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(pictureTextService.deletePictureTextByIds(ids));
    }

    @PostMapping("/get")
    @ResponseBody
    public AjaxResult getList() {
        PictureText pictureText = new PictureText();
        return success(pictureTextService.selectPictureTextList(pictureText));
    }


    @PostMapping("/getitem")
    @ResponseBody
    public AjaxResult getitem(@RequestParam("ID") Long id) {
        PictureText pictureText = new PictureText();
        return success(pictureTextService.selectPictureTextById(id));
    }


    @PostMapping("/adduser")
    @ResponseBody
    public AjaxResult addpicture(@RequestParam("file") MultipartFile picture,
                                 PictureText pictureText) {
        FileUploadUtil fileUploadUtil = new FileUploadUtil();
        String Url = fileUploadUtil.updateFileReturnPath(picture);
        pictureText.setPictureSrc(Url);
        return success(pictureTextService.insertPictureText(pictureText));
    }


}
